<?php 
	include 'base.php';
	Class RandPAction extends Action{
		Public function insert(){
            $ValidationModel = new ValidationModel();
            if($_POST['submit']=="录入"){
//                p($_POST);
                $randpDup= $ValidationModel->Duplicate('Information','num',$_POST['num']);
                if ($randpDup==null){
                    $this->error('无此学生或未录入此学生信息!');
                }
                $randpVal = $ValidationModel->validation($_POST);
                if ($randpVal!=-1){
                    $this->error($randpVal);
                }
				$rp = M('randp');
				$gra = M('grade');
				if($_POST['addGrade']!=""){
					if($_POST['addReason']==""){
						$this->error("加分原因不能为空!");
					}
					else{
						$data = array(
						'num' => $_POST['num'],
						'addGrade'=>$_POST['addGrade'],
						'addReason' =>$_POST['addReason'],
						);
						// var_dump($_POST);
						$where['num'] = $_POST['num'];
						$where['classId'] = '11';
						$sel = $gra->where($where)->select();
						// var_dump($sel);
						foreach ($sel as $v) {}
						// var_dump($v['grade']);
						


						$upd1 = array(
								'grade' => ($v['grade']+$_POST['addGrade']),
							);

						$update = $gra ->data($upd1) ->where("classId=11")->save();
						$add = $rp->data($data)->add();
						if($add&&$update){
								$pd = '1';
						}
					}
				}
				
				if($_POST['subGrade']!=""){
					if($_POST['subReason']==""){
						$this->error("减分原因不能为空!");
					}
					else{
						$data = array(
						'num' => $_POST['num'],
						'subGrade'=>(0-$_POST['subGrade']),
						'subReason' =>$_POST['subReason'],
						);

						$where['num'] = $_POST['num'];
						$where['classId'] = '11';
						$sel = $gra->where($where)->select();
						foreach ($sel as $v2) {}

						$upd2 = array(
								'grade' => ($v2['grade']-$_POST['subGrade']),
							);

						$update2 = $gra ->data($upd2) ->where("classId=11")->save();
						$add2 = $rp->data($data)->add();
						if($add2&&$update2){
								$pd = '1';
						}
					}
				}
                if ($_POST['addGrade']==""&&$_POST['subGrade']==""){
                    $this->error("无任何操作!");
                }
				if($pd=='1'){
					$this->success("录入成功!活动分数已计算");
				}
			
				// $this->display();
			}
			else{
				$this->display();
			}
			
		}

		Public function select(){
			$where['username'] = $_GET['username'];
			$this->qx = M('user')->where($where)->select();
            $ValidationModel = new ValidationModel();

            if($_POST['submitS']!=""){
                $randpInfo = $ValidationModel->Duplicate('Information','num',$_POST['num']);
                $randpInn = $ValidationModel->Duplicate('Innovate','num',$_POST['num']);
                if ($randpInn==null){
                    if ($randpInfo==null){
                        $this->error('无此学生或未录入此学生信息!');
                    }
                    $this->error('此学生无奖惩记录');
                }else{
                    $rp = M('randp');
                    $this->reason = $rp->where("num=$_POST[num]")->select();
                }
                //BUG : 输入正确后输入错误会显示上一次正确的结果!!
				// $this->display();
			}
			if($_POST['submitI']!=""){
				$mis = M('mistake');

				$data = array(
						'num' => $_POST['num'],
						'content' => $_POST['text'],
					);
				$insert = $mis->data($data)->add();
				if($insert){
					$this->success("您的反馈已成功提交!");
				}

			}
			else{
				$this->display();
			}
			
		}
	}
 ?>